from time import sleep

from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


# 官方文档 https://www.selenium.dev/zh-cn/documentation/webdriver/getting_started/first_script/

# 通过 xpath 判断元素是否存在
def is_element_present(driver, by, value):
    try:
        driver.find_element(by, value)
        return True
    except exceptions.NoSuchElementException:
        return False


# 删除所有kafka topic
def clean_kfka(url):
    options = Options()
    options.binary_location = "seleniumtools/chrome-linux/chrome"
    service = Service("seleniumtools/chromedriver_linux64/chromedriver")
    with webdriver.Chrome(options=options, service=service) as driver:
        driver.implicitly_wait(0.5)
        driver.maximize_window()
        driver.get(url)
        wait = WebDriverWait(driver, timeout=1)

        is_exist = is_element_present(driver, By.LINK_TEXT, "backTopic")
        if is_exist:
            driver.find_element(By.LINK_TEXT, "backTopic").click()
            driver.find_element(By.TAG_NAME, "button").click()
            wait.until(expected_conditions.alert_is_present())
            alert = driver.switch_to.alert
            # print(alert.text)
            alert.accept()

        is_exist = is_element_present(driver, By.LINK_TEXT, "nrljTopic")
        if is_exist:
            driver.find_element(By.LINK_TEXT, "nrljTopic").click()
            driver.find_element(By.TAG_NAME, "button").click()
            wait.until(expected_conditions.alert_is_present())
            alert = driver.switch_to.alert
            # print(alert.text)
            alert.accept()

        is_exist = is_element_present(driver, By.LINK_TEXT, "streamData")
        if is_exist:
            driver.find_element(By.LINK_TEXT, "streamData").click()
            driver.find_element(By.TAG_NAME, "button").click()
            wait.until(expected_conditions.alert_is_present())
            alert = driver.switch_to.alert
            # print(alert.text)
            alert.accept()

        is_exist = is_element_present(driver, By.LINK_TEXT, "tour")
        if is_exist:
            driver.find_element(By.LINK_TEXT, "tour").click()
            driver.find_element(By.TAG_NAME, "button").click()
            wait.until(expected_conditions.alert_is_present())
            alert = driver.switch_to.alert
            # print(alert.text)
            alert.accept()

        # input("按任意键退出...")


# 删除所有neo4j数据
def clean_neo4j(url, pwd):
    options = Options()
    options.binary_location = "seleniumtools/chrome-linux/chrome"
    service = Service("seleniumtools/chromedriver_linux64/chromedriver")
    with webdriver.Chrome(options=options, service=service) as driver:
        driver.implicitly_wait(0.5)
        driver.maximize_window()
        driver.get(url)
        # 如果发现有登录按钮
        not_login = is_element_present(driver, By.XPATH,
                                       '//*[@id="mount"]/div/div/div/div[2]/div/div[3]/article/div[2]/div/div[1]/div/div[2]/form/button', )
        if not_login:
            pwd_input = driver.find_element(By.XPATH,
                                            '//*[@id="mount"]/div/div/div/div[2]/div/div[3]/article/div[2]/div/div[1]/div/div[2]/form/div[3]/input', )
            pwd_input.clear()
            pwd_input.send_keys(pwd)
            driver.find_element(By.XPATH,
                                '//*[@id="mount"]/div/div/div/div[2]/div/div[3]/article/div[2]/div/div[1]/div/div[2]/form/button', ).click()
            print("登录成功")

        cmd_input = driver.find_element(By.XPATH,
                                        '//*[@id="mount"]/div/div/div/div[2]/div/div[1]/div[1]/div/div/div[6]/div[1]/div/div/div/div[5]/div/pre', )
        cmd_input.click()
        clean_all = "match (n) DETACH  delete n ;"
        print("----------------------- 命令 ----------------------")
        print(clean_all)
        ActionChains(driver).move_to_element(cmd_input).send_keys(clean_all).pause(1).perform()

        # print(is_element_present(driver, '//*[@id="mount"]/div/div/div/div[2]/div/div[1]/div[2]/span[3]'))
        driver.find_element(By.XPATH, '//*[@id="mount"]/div/div/div/div[2]/div/div[1]/div[2]/span[3]').click()
        sleep(1)
        handle = driver.current_window_handle
        input("请耐心等待执行完成，之后再任意键继续...")
        driver.switch_to.window(handle)


# 删除所有kafka topic
def clean_shouye(url, user, pwd):
    options = Options()
    options.binary_location = "seleniumtools/chrome-linux/chrome"
    service = Service("seleniumtools/chromedriver_linux64/chromedriver")
    with webdriver.Chrome(options=options, service=service) as driver:
        driver.implicitly_wait(0.5)
        driver.maximize_window()
        driver.get(url)

        input("后端启动较慢，请耐心等待验证码刷新显示正常，之后在等5秒，就可以任意键继续了...")
        # 等待登录页面
        not_login = is_element_present(driver, By.XPATH,
                                       '/html/body/app-root/app-login/div/div[2]/div[2]/div[2]/div/span', )
        if not_login:
            user_input = driver.find_element(By.XPATH,
                                             '/html/body/app-root/app-login/div/div[2]/div[2]/div[1]/form/nz-form-item[1]/nz-form-control/div/div/nz-input-group/input', )
            user_input.clear()
            user_input.send_keys(user)

            pwd_input = driver.find_element(By.XPATH,
                                            '/html/body/app-root/app-login/div/div[2]/div[2]/div[1]/form/nz-form-item[2]/nz-form-control/div/div/nz-input-group/input', )
            pwd_input.clear()
            pwd_input.send_keys(pwd)

            captcha_input = driver.find_element(By.XPATH,
                                                '/html/body/app-root/app-login/div/div[2]/div[2]/div[1]/form/nz-form-item[3]/nz-form-control/div[1]/div/nz-input-group/input', )
            captcha_input.clear()
            captcha_input.send_keys('1111')

            driver.find_element(By.XPATH, '/html/body/app-root/app-login/div/div[2]/div[2]/div[2]/div/span', ).click()
            # print("登录成功")

        sleep(3)
        goto_admin = driver.find_element(By.XPATH, "/html/body/app-root/app-page-layout/div/div[1]/div[1]/img")
        goto_admin.click()

        class1_menu = driver.find_element(By.XPATH,
                                          "/html/body/app-root/app-layout/nz-layout/nz-sider/div/app-layout-sidebar/ul/li[3]/div[1]/span")
        class1_menu.click()

        class2_menu = driver.find_element(By.XPATH,
                                          "/html/body/app-root/app-layout/nz-layout/nz-sider/div/app-layout-sidebar/ul/li[3]/div[2]/ul/li[2]/div[1]/span")
        class2_menu.click()
        class3_menu = driver.find_element(By.XPATH,
                                          "/html/body/app-root/app-layout/nz-layout/nz-sider/div/app-layout-sidebar/ul/li[3]/div[2]/ul/li[2]/div[2]/ul/li[5]/span")
        class3_menu.click()

        gx_clean_btn = driver.find_element(By.XPATH,
                                           '/html/body/app-root/app-layout/nz-layout/nz-layout/nz-content/div/app-merge-log/div/div[2]/div[1]/div[1]/button[1]')
        gx_clean_btn.click()
        sleep(2)

        tupu_clean_btn = driver.find_element(By.XPATH,
                                             '/html/body/app-root/app-layout/nz-layout/nz-layout/nz-content/div/app-merge-log/div/div[2]/div[1]/div[1]/button[2]')
        tupu_clean_btn.click()
        # input("请耐心等待执行完成，之后再任意键继续...")
        sleep(2)

        pz_clean_btn = driver.find_element(By.XPATH,
                                           '/html/body/app-root/app-layout/nz-layout/nz-layout/nz-content/div/app-merge-log/div/div[2]/div[1]/div[1]/button[3]')
        pz_clean_btn.click()
        sleep(2)

        gljd_clean_btn = driver.find_element(By.XPATH,
                                             '/html/body/app-root/app-layout/nz-layout/nz-layout/nz-content/div/app-merge-log/div/div[2]/div[1]/div[1]/button[4]')
        gljd_clean_btn.click()
        # input("请耐心等待执行完成，之后再任意键继续...")
        sleep(8)


if __name__ == "__main__":
    # clean_kfka(kafdrop_url)
    print()
